package main.test.SparkAPI.Baics.classApi

/**
 * CHapter 2  Object oriented fundamentals
 *
 * in unit 1,reviewed basic concepts of OOP
 * in this unit, keep leaning its fundats.
 * implement a sbt executable application to return the current
 * date and time for a given time zone using java.time.
 * one of the internanl packages available .
 * In particular, you will learn about the following subjects:
 *        Lesson 9 demonstrates how to structure your program in logical packages and
 *        import internal and external ones in your code.
 *        Lession 10 shows you how to use access modifiers to control the
 *        scope of your values, func, and clases, and to prevent your program form
 *        exposing information and unwanted modifications.
 *        Lesson 11 illustrates objects as built in singleton in the language.
 *        dicover how to implement static methods and new constructs for a given class through its
 *        companion object.
 *        Lesson 12 introduces trait as a tool to define commonalities between
 *        between different classes, on of its most popular use case.
 *        You are probaley aready familiar with this concept; many object-oriented languages refer to it as interface.
 *        finally,apply these concepts and implement an sbt app to return tiem in
 *        give zone in Lesson 13.
 *        once lean to create sbt app, continue with unit3, see how to
 *        implement a simple HTTP server.
 *
 */
object OOrientedFundas {
  /**
   * import and reuse code from other packages
   * structure you programe in logical packages
   * now that you rreviewd basic oop in unit1.
   * you will learn about packages. They allow you to organize you software in
   * logical groups to navigate your codebase more easily. By importing them
   * you can reuse them in server parts of your application.
   * You can also publish them,making their code avalable on line for ohter people
   * to download and use; you refer to a publised package as libray,
   * In the capstone, you create a dedicted logical unit for you tiem application and you will import
   * and use an internal package called java.time.
   */
  // import an exisiting package.
  /**
   * image want to write a prog to read the content of file into a str,
   * it would be challenging  without the use of a package or lib.
   * YOu need to identify location of meamory sellls for each binary file.
   * read and convert them to text after applying a particular encoding ,
   * also need to handle many edge case, such as try to access a file could be corrupted or misisng
   * or use an unexpected ending.But you don't have to deal with such low level
   * oper, use internal package to do it.
   *
   */
  //it show how you do it by scala.io module
  import scala.io.Source
  // it have Source from scala.io

  // return a bufferedSource ,which is a reference to the file
  // reads the file and creates instance of type Iterator[String]
  // by parsing each line of the file a string
  //concates all string using char "\n"
  def readFileIntoString(filename:String): String = {
    Source.fromFile(filename).getLines().mkString("\n")

    /**
     * different usage of scala imports
     * It imports everything in path. import path._
     *  It imports element in path. import path.element
     *  import path.element._
     *  It imports every code component
     *  from an element in path.
     *  import path.element.element
     *  It imports a specific code element
     *  from an element in path.
     *  It creates an alias for element in path. import path. {e l e m e n t = > a l i a s }
     *  It imports two elements in path. import path. {e l e m e n t , e l e m e n t }
     *  path.{element => alias}
     *  path.{element, element}
     */


  }
  def myFuncs()  = {
    import java.sql.{Date => SqlDate}
    val date = SqlDate.valueOf("2019-09-01")
    println(date.getYear)
  }

  /**
   * access modifier
   * track the guests and the costs of a paarty
   * Peple register for the event should be only publicly accessible functionaliy
   * The follow code provides a possible implementation.
   *
   */
    class Party{
    // making it no longer accessible outside the class Party
    private var attendees = 0
    def register(gusts:Int) =
      attendees += gusts

  }


  class Person {
    private val age = 18
  }
  class Student extends Person

  val ps = new Student
//  ps.age

  class EVENT {
    protected  def estimateCosts(attendees:Int) :Double = {
      println("calling estimateCost Fucnt with people "+ attendees)

      if(attendees <10) 50.00 else attendees * 12.3424
    }
  }

  class PartyUDF extends EVENT {
    private  var attendees = 0


    def cost: Double = estimateCosts(attendees)

    def register(guests:Int): Unit = {
      attendees += guests
      println("now have "+attendees+" people")
    }

  }

  def testUDFpARTY() ={
    val x = new PartyUDF()
    x.register(5)
    println("cost is ==> "+x.cost+" ")
    x.register(7)
    println("cost is ==> "+x.cost)
//    println("cost is ==> "+x.getCosts)

  }





  def main(args: Array[String]): Unit = {
    myFuncs()
    testUDFpARTY()

  }




}
